package com.example.shop.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop.persistence.entity.User;
import com.example.shop.persistence.mapper.UserMapper;
import com.example.shop.modules.sys.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shop.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static com.example.shop.utils.JSONResult.renderError;
import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author jason
 * @since 2019-12-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;


    /*
    用户登录
     */
    @Override
    public JSONResult loginUser(String username,String phone, String password){
        QueryWrapper ew = new QueryWrapper();
        if(!StringUtils.isEmpty(username)){
            ew.eq("username",username);
        }else{
            ew.eq("phone",phone);
        }
        User user = userMapper.selectOne(ew);
        // 如果用户信息不为空
        if (user == null) {
            return renderError("用户不存在，登陆失败！");
        }
        // 用户密码相比较
        if(user.getPassword().equals(password)){
            user.setPassword(null);
            return renderSuccess(user);
        }
        return renderError("密码错误！");
    }
}
